//
//  CampusPostQuestionViewController.m
//  ShowYork
//
//  Created by hxh on 2021/3/29.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "CampusPostQuestionViewController.h"
#import "CampusQuestionDetailModel.h"
@interface CampusPostQuestionViewController ()<UITextViewDelegate>
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIButton *postBtn;
@property (nonatomic,strong)UITextView *textView;
@property (nonatomic,strong)UILabel *maxCountLb;
@end

@implementation CampusPostQuestionViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = NO;

}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
  
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    
    // Do any additional setup after loading the view.
}
- (void)setUI{
    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:C102 forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = font(17);
    [self.cancelBtn addTarget:self action:@selector(cancelMethod) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.cancelBtn];
    
    self.postBtn = [[UIButton alloc]init];
    [self.postBtn setTitle:@"发布" forState:UIControlStateNormal];
    [self.postBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.postBtn.backgroundColor = RGBA(224, 224, 224, 1);
    self.postBtn.titleLabel.font = font(15);
    self.postBtn.cl_size = CGSizeMake(60, 30);
    self.postBtn.layer.cornerRadius = 15;
    self.postBtn.layer.masksToBounds = YES;
    self.postBtn.userInteractionEnabled = NO;
    [self.postBtn addTarget:self action:@selector(postMethod) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.postBtn];
    
    self.textView = [[UITextView alloc]init];
    self.textView.textColor = RGBA(107, 107, 107, 1);
    self.textView.font = font(14);
    self.textView.delegate = self;
    [self.view addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-16);
        make.top.left.equalTo(self.view).offset(16);
        make.height.mas_equalTo(200);
    }];
    self.maxCountLb = [[UILabel alloc]init];
    self.maxCountLb.text = @"0/200";
    self.maxCountLb.textColor = RGBA(229, 229, 229, 1);
    self.maxCountLb.font = font(14);
    [self.view addSubview:self.maxCountLb];
    [self.maxCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textView.mas_bottom).offset(16);
        make.right.equalTo(self.textView);
    }];
    
    [self.textView becomeFirstResponder];
}

- (void)cancelMethod{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)postMethod{
    
    [self.view endEditing:YES];
    
    if (![RPAppManager isLogin]) {
        [MGJRouter openURL:@"local://ddup/unLogin" withUserInfo:@{@"navigationVC":self.navigationController} completion:nil];
    }
    
    [SVProgressHUD show];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"md5"] = [RPAppManager md5StrWithApi:AddSchoolQuestion];
    param[@"questionText"] = self.textView.text;
    param[@"schoolId"] = self.schId;
    param[@"userId"] = [RPAppManager getCustomerId];
    [[RPNetworkExtensionTool sharedTool] requestPOST:kGET_REQUEST_URL(AddSchoolQuestion) parames:param success:^(id responseObj) {
         
        [WHToast showImage:[UIImage imageNamed:@"toast_tag_success"] message:@"发布成功" duration:2 finishHandler:nil];
    
        CampusQuestionDetailModel *dtlModel = [CampusQuestionDetailModel mj_objectWithKeyValues:responseObj[RESPONSE]];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateQuestionList" object:nil userInfo:@{@"data":dtlModel}];
        
        
        [self.navigationController popViewControllerAnimated:YES];
    
    } failure:^(NSError *error) {
        [WHToast showErrorWithMessage:error.userInfo[@"info"] duration:2 finishHandler:nil];
    }];
        

}

- (void)textViewDidChange:(UITextView *)textView{
    
    
    NSString *toBeString = textView.text;
 
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
 
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > 200){
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:200];
            if (rangeIndex.length == 1){
                textView.text = [toBeString substringToIndex:200];
                if (toBeString.length > 200) {
                    [WHToast showMessage:@"已超过字数上限" duration:2 finishHandler:nil];
                }
            }else{
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0,200)];
                textView.text = [toBeString substringWithRange:rangeRange];
           
                if (rangeRange.length < toBeString.length) {
                    [WHToast showMessage:@"已超过字数上限" duration:2 finishHandler:nil];
                }
            }
        }
    }
    if ([textView.text cl_removeSpaceString].length > 0) {
        self.postBtn.userInteractionEnabled = YES;
        self.postBtn.backgroundColor = RGBA(86, 128, 250, 1);
    }else{
        self.postBtn.userInteractionEnabled = NO;
        self.postBtn.backgroundColor = RGBA(224, 224, 224, 1);
    }
    self.maxCountLb.text = [NSString stringWithFormat:@"%lu/200",MIN(200, textView.text.length)];
}


@end
